package com.example.android_app.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.android_app.R;
import com.example.android_app.database.MyDataBase;
import com.example.android_app.databinding.ActivityRegisterBinding;


public class RegisterActivity extends AppCompatActivity {

    MyDataBase myDataBase = new MyDataBase(RegisterActivity.this, "DB", 1);

    ActivityRegisterBinding activityRegisterBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.noToolbar);
        super.onCreate(savedInstanceState);
        activityRegisterBinding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(activityRegisterBinding.getRoot());
        commit();

    }

    private void commit() {
        activityRegisterBinding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username_value = activityRegisterBinding.username.getText().toString().trim();
                String password_value = activityRegisterBinding.password.getText().toString().trim();

                if (isValid(username_value, password_value,null)){

                    myDataBase.addData(username_value, password_value);
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }

    private boolean isValid(String username_value, String password_value, String confirmPassword_value) {
        if (TextUtils.isEmpty(username_value)){
            Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return false;
        }else if(TextUtils.isEmpty(password_value)) {
            Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }else{
            return true;
        }
//        }else if (password_value.equals(confirmPassword_value)) {
//            return true;
//        }
//        }else{
//            Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
//            return false;
//        }

    }

}
